/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.app;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddGroupReq;
import org.cshoo.tattoo.authorize.api.dto.AddNewRoleReq;
import org.cshoo.tattoo.authorize.api.dto.AddNewUserReq;
import org.cshoo.tattoo.authorize.api.dto.UpdateGroupReq;
import org.cshoo.tattoo.authorize.domain.po.Group;
import org.cshoo.tattoo.authorize.domain.po.Role;
import org.cshoo.tattoo.authorize.domain.po.User;
import org.cshoo.tattoo.authorize.domain.service.GroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
@Slf4j
public class GroupAppService {

    @Resource
    private GroupService groupService;

    /**
     * 添加一个群组
     *
     * @param addGroupReq 添加群组请求
     */
    public void addGroup(AddGroupReq addGroupReq) {
        Group group = new Group();
        BeanUtils.copyProperties(addGroupReq, group);
        groupService.addGroup(group);
    }

    /**
     * 在群组中增加一个新用户
     *
     * @param addNewUserReq 添加新用户请求
     */
    public void addNewUser(AddNewUserReq addNewUserReq) {
        User user = new User();
        user.setUsername(addNewUserReq.getUsername());
        user.setName(addNewUserReq.getName());
        Long groupId = addNewUserReq.getGroupId();
        groupService.addNewUser(groupId, user);
    }

    /**
     * 添加现有用户
     *
     * @param groupId 群组id
     * @param userId  用户id
     */
    public void addExistingUser(Long groupId, Long userId) {
        groupService.addExistingUser(groupId, userId);
    }

    /**
     * 新建一个新角色并关联到指定群组
     *
     * @param addNewRoleReq 添加新的角色请求
     */
    public void addNewRole(AddNewRoleReq addNewRoleReq) {
        Role role = new Role();
        role.setName(addNewRoleReq.getName());
        role.setDescription(addNewRoleReq.getDescription());
        Long groupId = addNewRoleReq.getGroupId();
        groupService.addNewRole(groupId, role);
    }

    /**
     * 分配一个现有的角色
     *
     * @param groupId 群组id
     * @param roleId  角色id
     */
    public void assignExistingRole(Long groupId, Long roleId) {
        groupService.assignExistingRole(groupId, roleId);
    }

    /**
     * 解除一个角色
     *
     * @param groupId 群组id
     * @param roleId  角色id
     */
    public void dismissRole(Long groupId, Long roleId) {
        groupService.dismissRole(groupId, roleId);
    }

    /**
     * 移除一个用户
     *
     * @param groupId 群组id
     * @param userId  用户id
     */
    public void removeUser(Long groupId, Long userId) {
        groupService.removeUser(groupId, userId);
    }

    /**
     * 修改一个群组
     * 这里只修改基本信息，
     * 当不传角色的时候不修群组对应的角色，防止将角色清空
     *
     * @param updateGroupReq 修改群组请求对象
     */
    public void updateGroup(UpdateGroupReq updateGroupReq) {
        Group group = new Group();
        BeanUtils.copyProperties(updateGroupReq, group);
        groupService.updateGroup(group);
    }

    /**
     * 查询所有群组
     *
     * @return 群组
     */
    public List<Group> findAllGroup() {
        return groupService.findAllGroup();
    }

    /**
     * 删除一个群组
     *
     * @param id 群组id
     */
    public void deleteGroup(Long id) {
        groupService.deleteGroup(id);
    }
}
